/*
 * SortMenu.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;

/** 店铺详情-全部商品页面的排序菜单 */
public class SortMenu extends LinearLayout implements View.OnClickListener {

    public static final int MENU_NEWEST = 0;

    public static final int MENU_PRICE = 1;

    public static final int MENU_SALES = 2;

    private Context mContext;

    private TextView mTvNewest;

    private RelativeLayout mRlytPrice;

    private RelativeLayout mRltySales;

    private ImageView mIvPriceUp;

    private ImageView mIvPriceDown;

    private TextView mTvSales;

    private TextView mTvPrice;

    private ImageView mIvSalesDown;

    private RelativeLayout mRlytGridStyle;

    private ImageView mIvGridStyle;

    private OnSortMenuClickListener mListener;

    /** 上一次点击的菜单 */
    private int lastClickMenu;

    /** 当前选中的菜单 */
    private int currentMenu;

    private boolean currentSortUp;

    private boolean isGridStyle;

    public SortMenu(Context context) {
        this(context, null);
    }

    public SortMenu(Context context, AttributeSet attrs) {
        super(context, attrs);

        mContext = context;

        initViews(mContext);

        // 为实现默认从高到低的效果，先使用从低到高
        currentSortUp = true;

        // 默认选择最新
        clearSelected();
        mTvNewest.setSelected(true);

        // 默认为网格状布局
        isGridStyle = false;
        mIvGridStyle.setSelected(false);
    }

    private void initViews(Context context) {
        View root = LayoutInflater.from(mContext).inflate(R.layout.item_sort_menu, this);

        mTvNewest = (TextView) root.findViewById(R.id.tv_newest);
        mRlytPrice = (RelativeLayout) root.findViewById(R.id.rlyt_price);
        mRltySales = (RelativeLayout) root.findViewById(R.id.rlyt_sales);
        mIvPriceUp = (ImageView) root.findViewById(R.id.iv_price_up);
        mIvPriceDown = (ImageView) root.findViewById(R.id.iv_price_down);
        mTvSales = (TextView) root.findViewById(R.id.tv_sales);
        mTvPrice = (TextView) root.findViewById(R.id.tv_price);
        mIvSalesDown = (ImageView) root.findViewById(R.id.iv_sales_down);
        mRlytGridStyle = (RelativeLayout) root.findViewById(R.id.rlyt_grid_style);
        mIvGridStyle = (ImageView) root.findViewById(R.id.iv_grid_style);

        mTvNewest.setOnClickListener(this);
        mRltySales.setOnClickListener(this);
        mRlytPrice.setOnClickListener(this);
        mRlytGridStyle.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.rlyt_grid_style) {
            // 改变列表样式
            if (mListener != null) {
                isGridStyle = !isGridStyle;
                mIvGridStyle.setSelected(isGridStyle);
                mListener.onChangeListStyle(isGridStyle);
            }
            return;
        }

        clearSelected();
        switch (v.getId()) {
            case R.id.tv_newest:
                mTvNewest.setSelected(true);
                currentMenu = MENU_NEWEST;

                if (lastClickMenu == MENU_NEWEST) {
                    return;
                }
                lastClickMenu = MENU_NEWEST;

                if (mListener != null) {
                    mListener.onSortMenuClick(MENU_NEWEST, false);
                }
                break;
            case R.id.rlyt_price:
                lastClickMenu = MENU_PRICE;
                currentMenu = MENU_PRICE;
                mTvPrice.setSelected(true);
                if (currentSortUp) {
                    mIvPriceDown.setSelected(true);
                    if (mListener != null) {
                        mListener.onSortMenuClick(MENU_PRICE, false);
                    }
                } else {
                    mIvPriceUp.setSelected(true);
                    if (mListener != null) {
                        mListener.onSortMenuClick(MENU_PRICE, true);
                    }
                }
                currentSortUp = !currentSortUp;
                break;
            case R.id.rlyt_sales:
                currentMenu = MENU_SALES;
                mTvSales.setSelected(true);
                mIvSalesDown.setSelected(true);
                if (lastClickMenu == MENU_SALES) {
                    return;
                }
                lastClickMenu = MENU_SALES;

                if (mListener != null) {
                    mListener.onSortMenuClick(MENU_SALES, false);
                }
                break;
        }
    }

    private void clearSelected() {
        mTvSales.setSelected(false);
        mTvNewest.setSelected(false);
        mTvPrice.setSelected(false);
        mIvPriceUp.setSelected(false);
        mIvPriceDown.setSelected(false);
        mIvSalesDown.setSelected(false);
    }

    /**
     * 返回当前价格排序的规则
     *
     * @return true表示升序，false为降序
     */
    public boolean isSortUp() {
        return currentSortUp;
    }

    /** 返回当前选中的菜单 */
    public int getCurrentMenu() {
        return currentMenu;
    }

    public void setOnSortMenuClickListener(OnSortMenuClickListener listener) {
        mListener = listener;
    }

    /** 排序菜单点击的回掉 */
    public interface OnSortMenuClickListener {

        /**
         * 只有点击销量时第二个参数才有用.
         *
         * @param menu 点击的菜单
         * @param isSortUp 是否为升序
         */
        void onSortMenuClick(int menu, boolean isSortUp);

        /**
         * 改变列表样式
         *
         * @param isGridStyle 是否是网格状列表
         */
        void onChangeListStyle(boolean isGridStyle);
    }
}
